import CryptoJS from 'crypto-js';

export default {
  //加密

  encrypt(word, keyStr, ivNum) {
    // console.log(word,keyStr,'///++');

    // secretKey:7kSVwvjDkJkVTYTf
    // secretId:O0LccqG811e3w0A8
    keyStr = keyStr ? keyStr : '2946301785ajkbgd6541298470iljtem';

    let key = CryptoJS.enc.Utf8.parse(keyStr);

    // let iv = CryptoJS.enc.Utf8.parse('O0LccqG811e3w0A8'); //十六位十六进制数作为秘钥偏移量(ABCDEF1234123412)
    ivNum = CryptoJS.enc.Utf8.parse(ivNum ? ivNum : 'O0LccqG811e3w0A8'); //十六位十六进制数作为秘钥偏移量(ABCDEF1234123412)
    // const iv = generatekey(16);

    let srcs = CryptoJS.enc.Utf8.parse(word);

    let encrypted = CryptoJS.AES.encrypt(srcs, key, {
      iv: ivNum,
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.Pkcs7,
    });

    // return encrypted.ciphertext.toString().toUpperCase();
    return encrypted.toString();
  },

  //解密

  decrypt(word, keyStr, ivNum) {
    // console.log(word,keyStr,'///');
    keyStr = keyStr ? keyStr : '2946301785ajkbgd6541298470iljtem';

    let key = CryptoJS.enc.Utf8.parse(keyStr);

    // let iv = CryptoJS.enc.Utf8.parse('O0LccqG811e3w0A8'); //十六位十六进制数作为秘钥偏移量(ABCDEF1234123412)
    ivNum = CryptoJS.enc.Utf8.parse(ivNum ? ivNum : 'O0LccqG811e3w0A8');

    // var encryptedHexStr = CryptoJS.enc.Hex.parse(word);

    // var srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
    let srcs = word;

    let decrypt = CryptoJS.AES.decrypt(srcs, key, {
      iv: ivNum,
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.Pkcs7,
    });

    let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);

    return decryptedStr.toString();
  },
};
